define(function(require, exports, module) {
	// require('./plug/hammer.min');
	// require('./plug/swiper.jquery.min');
	// require('./plug/lazyload_echo');
	//require('shop');
	var myPub = require('./public');

	var oHeader = $('.js_header'); //头部
	var oFooter = $('.js_footer'); //底部
	var oContain = $('.js_contain'); //主体
	var oHScroll = $('.js_HScroll'); //滚动产品
	var oGoods_showOne = $('.js_goods_showOne'); //一排产品
	var oGoods_showTwo = $('.js_goods_showTwo'); //并排产品

	oHeader.on("touchmove", function(e) { e.preventDefault() });
	oFooter.on("touchmove", function(e) { e.preventDefault() });

	var oHScroll = $('.js_HScroll'); //水平滚动 导航和产品
	var oHScroll_Ul = oHScroll.find('ul');
	var oHScroll_Li = oHScroll.find('li');

	//设置城市导航定位
	$(window).on('scroll', function() {
		myPub.throttle(setNavFixed, window, 50);
	})

	var oChangeCity = $('.js_changeCity')

	function setNavFixed() {
		$(window).scrollTop() > 25 ? oChangeCity.addClass('fixedDom') : oChangeCity.removeClass('fixedDom')
	}

	//设置产品固定宽度
	SetUlWidth();
	$(window).resize(function() { SetUlWidth(); });

	oHScroll.on('scroll', function() {
		//水平懒加载
		myPub.throttle(myPub.EchoRush, window);
	})

	myPub.SetImgHeight(oGoods_showTwo);

	function SetUlWidth() {
		//水平滚动li的宽度
		var oHScroll_Li_W = oHScroll_Li.outerWidth(true);
		oHScroll_Ul.css({
			'width': oHScroll_Li_W * oHScroll_Li.length
		});
	}

	//产品懒加载  异步加载 调用这个
	myPub.lazLoadingImgEcho();

	/**
	 * global ajaxLoad goods Length
	 */
	var goodsLen = 0;
	var pRdId = 0;

	module.exports = {
		init: function(rfId, childRfIds) {
			pRdId = rfId;
			startloadAjax(function () {
                myPub.SetImgHeight(oGoods_showTwo)
            });
			Swiper_indexAD();

		}
	};

	$('#more').show();
	//显示更多
	var oMoreBtn = $('.js_moreBtn'); //加载更多按钮

	var loadTime = null;
	var loadGoHeight = oGoods_showTwo.find('li').height();

	var pageSize = 20;
	var pageCurrent = 0;
	var baseInput = $('#baseInput').val();

	function startloadAjax(fn) {
		var bOff = false
		$.ajax({
			url: baseInput + "/regionFeature/findGoodsByParentRfId",
			dataType: 'json',
			method:'POST',
			data: { 'rfId': pRdId, 'start': (pageSize * pageCurrent), 'limit': pageSize },
			async: false,
			success: function(goodsList) {
				loadData(goodsList);
				if(goodsLen < pageSize) {
					bOff = true
					$('#more').hide();
					$('#noMore').show();
				}
			},
			complete:function(XHR) {
				XHR = null;
				if(fn)fn(bOff)
			}
		});

	}

	function hideMore() {
		$('#more').hide();
		$('#noMore').show();
	}

	function loadData(goodsList) {
		if(!goodsList) {
			return;
		}
		var gLen = goodsList.length;
		goodsLen = gLen;

		if(goodsLen < pageSize) {
			pageCurrent = 0;
		} else {
			pageCurrent++;
		}

		if(gLen == 0) {
			return;
		}
		var html = [];
		for(var i = 0; i < gLen; i++) {
			html.push('<li>');
			html.push('<a class="" href="/shop/goods/view/' + goodsList[i].goodsSn + '?goodschannel=regionfeature">');
			html.push('<figure>');
			html.push('<img class="js_lazyImg" data-echo="' + myPub.httpImageUrl + goodsList[i].showImage + '" src="/static_v3/image/loadImg.jpg">');
			html.push('<i class="tip-th" style="display: none;"></i>');
			html.push('</figure>');
			html.push('<aside class="priceArea ">');
			html.push('<div class="priceArea_name">');
			html.push('<h4>' + goodsList[i].name + '</h4>');
			html.push('<p class="smallFont_tip">' + goodsList[i].title + '</p>');
			html.push('</div>');
			html.push('<div class="priceArea_price">');
			html.push('<p class="HBox BoxCenter priceAreaBox">');
			html.push('<i>￥</i>');
			html.push('<span class="goodsPrice">' + goodsList[i].salePrice + '</span>');
			html.push('<span class="goodsWeight Bflex1">/' + goodsList[i].productName + '</span>');
			html.push('</p>');
			html.push('</div>');
			html.push('</aside>');
			html.push('</a>');
			html.push('<p class="addCar js_addCar" pId="' + goodsList[i].productId + '"><em class="icon-shopcar_small"></em></p>');
			html.push('</li>');
		}
		oGoods_showTwo.find('ul').append(html.join(''));
	}

	oMoreBtn.hammer().on('tap', function() {

		var _this = $(this);
		_this.hide().siblings('.js_loadingBtn').show().find('.icoStyle').addClass('myRotateRefresh');

		startloadAjax(function (bOff) {
            if(bOff) {
                _this.hide().siblings('.js_loadingBtn').hide().removeClass('myRotateRefresh');
                $('#noMore').show();
            }
            else {
                clearTimeout(loadTime);
                loadTime = setTimeout(function() {
                    _this.show().siblings('.js_loadingBtn').hide().find('.icoStyle').removeClass('myRotateRefresh');
                    //产品懒加载  异步加载 调用这个
                    myPub.lazLoadingImgEcho();
                    $(window).scrollTop() + loadGoHeight;
                }, 300)
            }
            myPub.SetImgHeight(oGoods_showTwo)

        });
		myPub.HamstopPropaga()
	})

	//广告轮播
	var sTime = null;
	var oSilderImg = $('.js_silderImg'); //广告轮播框
	var oBNext = $('.js_BNext'); //下一个
	var oBPrev = $('.js_BPrev'); //上一个
	var oPagin = $('.js_pagin'); //个数点

	function Swiper_indexAD() {
		var indexSwiperAD = oSilderImg.swiper({
			nextButton: oBNext,
			prevButton: oBPrev,
			pagination: oPagin,
			paginationClickable: false,
			preloadImages: false,
			lazyLoading: true,
			autoplay: 4000,
			onTouchEnd: function(swiper) {
				clearTimeout(sTime);
				sTime = setTimeout(function() {
					indexSwiperAD.startAutoplay();
				}, 4000);
			},
		});
	}

	function SetSession() {
		sessionStorage.setItem("indexKey", tabIndex);
		sessionStorage.setItem("pagearr", pagearr);
		sessionStorage.setItem("falgarr", falgarr);
	}

});